/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;
import View.StarClusterMainWindow;
import java.awt.Color;
import java.util.*;
//import javax.vecmath.*;

/**
 *
 * @author snowangelic
 */
public class DataSet {

    StarClusterMainWindow view;
    ArrayList<Action> actionList;
    ArrayList<Attribute> attributeList;
    ArrayList<Clusterization> clusterizationList;

    public DataSet(){
        actionList = new ArrayList<Action>();
        attributeList = new ArrayList<Attribute>();
        clusterizationList=new ArrayList<Clusterization>();
    }

    public void setView(StarClusterMainWindow mainWindow) {
        view=mainWindow;
    }

    // --- Accesseurs / Mutateurs / Informations pour les Actions ---

    /**
     *
     * @param attributes
     */
    /*
    public void addAction(ArrayList<AttributeValue> attributes){
        Action act=new Action(attributes);
        actionList.add(act);
    }
     * */

    public Action addAction(){
        Action act=new Action();
        actionList.add(act);
        view.dataSetChanged();
        return act;
    }

    /**
     *
     * @param index
     * @return
     */
    public Action getAction(int index){
        return actionList.get(index);
    }

    /**
     *
     * @return
     */
    public int getActionNbr(){
        return actionList.size();
    }

    public void setActionCoords(int index, float[] coords){
        getAction(index).setCoords(coords);
    }

    public float[]getActionCoords(int index){
        return getAction(index).getCoords();
    }

    public Color getActionColor(int index,Clusterization clus){
        return clus.getColor(getAction(index));
    }

    public float[] getClusterCoords(Clusterization clus, int clusterIndex) {
        return clus.getClusterCoords(clusterIndex);
    }

    public String getClusterName(Clusterization clus, int clusterIndex){
        return clus.getClusterName(clusterIndex);
    }

    public String getClusterName(int actionIndex,Clusterization clus){
        return clus.getClusterName(actionList.get(actionIndex));
    }

    // --- Accesseurs / Mutateurs / Informations pour les Attributs ---

    /**
     *
     * @param name
     * @param type
     */
    public void addAttribute(String name,AttributeType type){
            Attribute Attr=new Attribute(name,type);
            attributeList.add(Attr);
            view.dataSetChanged();
    }

    /**
     *
     * @return
     */
    public int getAttributeNbr(){
        return attributeList.size();
    }

    public int getNumericalAttributeNumber(){
        // On compte le nombre d'attributs de type numerical
        Iterator it=attributeList.iterator();
        int count =0;
        while(it.hasNext()){
           Attribute attr=(Attribute) it.next();
           if(attr.getType()==AttributeType.Numerical) count++;
        }
        return count;
    }

    
    /**
     *
     * @param index
     * @return
     */
    public Attribute getAttribute(int index){
        return attributeList.get(index);
    }

    public Attribute getNumericalAttribute(int n){
        // Retourne un pointeur vers le n ieme attribut Numerique
        Iterator it=attributeList.iterator();
        Attribute A=null;
        while(it.hasNext()){
           Attribute attr=(Attribute) it.next();
           if(attr.getType()==AttributeType.Numerical) n--;
           if(n==-1){
               A=attr;
               break;
           }
        }
        return A;
    }

    /**
     *
     * @param index
     * @return
     */
    public AttributeType getAttributeType(int index){
        return attributeList.get(index).getType();
    }

    /**
     *
     * @param attribute
     * @return
     */
    public AttributeType getAttributeType(Attribute attribute){
        int index=attributeList.indexOf(attribute);
        return attributeList.get(index).getType();
    }

    /**
     *
     * @param index
     * @return
     */
    public String getAttributeName(int index){
        return attributeList.get(index).getName();
    }

     // --- Accesseurs / Mutateurs / Informations pour les Valeurs des Attributs ---

    public void setValue(Action action,Attribute attribute,Object value){
        action.setValue(attribute,value);
        view.dataSetChanged();
    }

    public void removeValue(Action action, Attribute attribute) {
        action.removeValue(attribute);
    }

    public void setNormalizedValue(Action action,Attribute attribute,Object value){
        action.setNormalizedValue(attribute,value);
    }

    public void setNormalizedValue(int actionIndex,Attribute attribute,Object value){
        getAction(actionIndex).setNormalizedValue(attribute,value);
    }
    /**
     *
     * @param actionIndex
     * @param attributeIndex
     * @return
     */
    public Object getValue(int actionIndex,int attributeIndex){
        Attribute attr = getAttribute(attributeIndex);
        Action act = getAction(actionIndex);
        return act.getValue(attr);
    }

    public Object getValue(int actionIndex,int attributeIndex,boolean normalized){
        Attribute attr = getAttribute(attributeIndex);
        Action act = getAction(actionIndex);
        return act.getValue(attr,normalized);
    }

    public Object getValue(Clusterization clus,int clusterIndex,Attribute attribute,boolean normalized){
        return clus.getValue(clusterIndex,attribute,normalized);
    }

    public Object getValue(int actionIndex,Attribute attribute){
        Action act = getAction(actionIndex);
        return act.getValue(attribute);
    }

    public Object getValue(int actionIndex,Attribute attribute,boolean normalized){
        Action act = getAction(actionIndex);
        return act.getValue(attribute,normalized);
    }

    public float getMinVal(Attribute attr){
        return attr.getMin(this);
    }

    public float getMaxVal(Attribute attr){
        return attr.getMax(this);
    }

     // --- Accesseurs / Mutateurs / Informations pour les Kmeans ---
    /**
     *
     * @param kmeans
     */

    public void addClusterization(ClusterizationOptions clusOpt){
        if(clusOpt.getClusterizationAlgorithm()==ClusterizationAlgorithm.Kmeans)
            clusterizationList.add(new Kmeans((KmeansOptions) clusOpt));

        if(clusOpt.getClusterizationAlgorithm()==ClusterizationAlgorithm.Custom)
            clusterizationList.add(new Custom((CustomOptions) clusOpt));

        if(clusOpt.getClusterizationAlgorithm()==ClusterizationAlgorithm.Hierarchical)
            clusterizationList.add(new Hierarchical((HierarchicalOptions) clusOpt));

        view.dataSetChanged();
    }

    /**
     *
     * @return
     */
    public int getClusterizationNbr() {
        return clusterizationList.size();
    }

    /**
     *
     * @param index
     * @return
     */
    public Clusterization getClusterization(int index){
        return clusterizationList.get(index);
    }

    /**
     *
     * @param index
     * @return
     */
    public String getClusterizationName(int index){
        return getClusterization(index).getName();
    }

    public void deleteClusterization(int clusterizationIndex) {
        if(clusterizationIndex>=0 && clusterizationIndex<clusterizationList.size())
        clusterizationList.remove(clusterizationIndex);
        view.dataSetChanged();
    }

    public void setClusterName(Clusterization clusterization, int clusterIndex, String name) {
        clusterization.setClusterName(clusterIndex,name);
        view.dataSetChanged();
    }

    public void addWeight(Kmeans kmeans,Attribute attribute,float weightVal){
        kmeans.AddWeight(attribute, weightVal);
    }


}
